<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\MakeModel;
use app\admin\model\FieldModel;
use app\admin\model\CountryModel;
use app\admin\model\SeriesModel;
use app\admin\model\MatorModel;

class Series extends Base
{
	/**
	 * 制造商对象
	 * @return mixed
	 */
	public $make_obj;
	
	/**
	 * 国家对象
	 * @return mixed
	 */
	public $country_obj;
	
	/**
	 * 系列对象
	 * @return mixed
	 */
	public $series_obj;
	
	/**
	 * 车型对象
	 * @return mixed
	 */
	public $mator_obj;
	
	/**
	 * 构造函数
	 * @return mixed
	 */
	public function __construct()
	{
		parent::__construct();
		$this->make_obj = new MakeModel();
		$this->series_obj = new SeriesModel();
		$this->mator = new MatorModel();	
		$this->country_obj = new CountryModel();
	}
	
	/**
	 * 车型系列
	 * @return mixed
	 */
    public function lists()
    {
		$fieldData = array();
		$field = FieldModel::all();
		foreach($field as $_f){
			$fieldData[$_f['id']] = $_f;
		}
		$countryData = array();
		$country = CountryModel::all();
		foreach($country as $_c){
			$countryData[$_c['id']] = $_c;
		}
		$makeData = array();
		$make = MakeModel::all();
		foreach($make as $_m){
			$makeData[$_m['id']] = $_m;
		}
		$input = input('get.');
		if(isset($input['data'])){
			$data = $this->series_obj->getSeriesList($input);
			if(!empty($data['list']['data'])){
				foreach($data['list']['data'] as $key=>$val){
					$data['list']['data'][$key]['field_country_code'] = $fieldData[$val['field_id']]['field_code'].'-'.$countryData[$val['country_id']]['country_code'];
					$data['list']['data'][$key]['field_country_name'] = $fieldData[$val['field_id']]['field_name'].'-'.$countryData[$val['country_id']]['country_name'];
					$data['list']['data'][$key]['make_name'] = $makeData[$val['make_id']]['make_name'];
					if(is_numeric($val['create_time'])){
						$data['list']['data'][$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
					}
				}
			}
			$result = array('code'=>0,'msg'=>'','count'=>$data['list']['total'],'data'=>$data['list']['data']);
			return json($result);
		}else{
			$where = array();
			$where['export'] = 1;
			$data = $this->country_obj->getFieldCountryList($where);
			if(!empty($data['list'])){
				foreach($data['list'] as $key=>$val){
					$fieldObj = FieldModel::get($val['field_id']);
					$countryObj = CountryModel::get($val['country_id']);
					$data['list'][$key]['field_country_code'] = $fieldObj->field_code." - ".$countryObj->country_code;
					$data['list'][$key]['field_country_name'] = $fieldObj->field_name." - ".$countryObj->country_name;
				}
			}
			$this->assign('field_country',$data['list']);
			$this->assign('field', $fieldData);
			return view('index');
		}
    }
	
	/**
	 * 新增系列页面
	 * @return mixed
	 */
	public function add()
	{
		return view('add');
	}
	
	/**
	 * 编辑系列页面
	 * @return mixed
	 */
	public function edit()
	{
		$input = input('get.');
		$view = SeriesModel::get($input['id'])->toArray();
		if($view['from_time'] > 0){
			$view['from_time'] = date('Y-m-d',$view['from_time']);
		}else{
			$view['from_time'] = '';
		}
		if($view['to_time'] > 0){
			$view['to_time'] = date('Y-m-d',$view['to_time']);
		}else{
			$view['to_time'] = '';
		}
		$this->assign('view', $view);
		return view('edit');
	}
	
	/**
	 * 更新系列
	 * @return mixed
	 */
	public function save()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			if(isset($input['from_time'])){
				$input['from_time'] = strtotime($input['from_time']);
			}
			if(isset($input['to_time'])){
				$input['to_time'] = strtotime($input['to_time']);
			}
			if(isset($input['f_c_id'])){
				$where = array();
				$where['id'] = $input['f_c_id'];
				$data = $this->country_obj->getCountryRela($where);
				$input['field_id'] = $data[0]['field_id'];
				$input['country_id'] = $data[0]['country_id'];
			}
			if(isset($input['id']) && intval($input['id']) > 0){
				$input['update_time'] = time();
				$flag = $this->series_obj->save($input,['id'=>$input['id']]);
			}else{
				$input['create_time'] = time();
				$input['update_time'] = time();
				$this->series_obj->data($input);
				$flag = $this->series_obj->save();
			}
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新系列数据，请求不合法！');
		}
	}
	
	/**
	 * 删除系列
	 * @return mixed
	 */
	public function del()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$where = array();
			$where['series_id'] = $input['id'];
			$series = $this->mator->getMatorList($where);
			if(!empty($series['list'])){
				$result = [
					'code' => 0,
					'msg'  => '系列已经车型，不允许删除！',
					'list' => $input
				];
			}else{
				$field = SeriesModel::get($input['id']);
				$field->series_status = -1;
				$flag = $field->save();
				if($flag){
					$result = [
						'code' => 1,
						'msg'  => '请求成功',
						'list' => $input
					];
				}else{
					$result = [
						'code' => 0,
						'msg'  => '请求失败',
						'list' => $input
					];
				}
			}
			return json($result);
		}else{
			$this->error('更新系列数据，请求不合法！');
		}
	}
	
	/**
	 * 更新系列状态
	 * @return mixed
	 */
	public function setStatus()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$field = SeriesModel::get($input['id']);
			$status = 1;
			if($field->series_status){
				$status = 0;
			}
			$field->series_status = $status;
			$flag = $field->save();
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新系列数据，请求不合法！');
		}
	}
	
}
